package com.eightbears.beboy.ui.user.activity;

import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.eightbears.beboy.BuildConfig;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.status.Status_Error;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_FEEDBACK;

public class FeedbackActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.et_feed_content)
    EditText et_feed_content;
    @BindView(R.id.tv_font_count)
    TextView tv_font_count;
    @BindView(R.id.et_feed_phone)
    EditText et_feed_phone;
    @BindView(R.id.tv_post_feed)
    TextView tv_post_feed;
    @BindView(R.id.tv_device)
    TextView tv_device;
    @BindView(R.id.tv_version)
    TextView tv_version;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private String maxInputFormat;
    private String maxInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_version.setText(BuildConfig.VERSION_NAME);
        tv_device.setText(Build.MODEL);
        et_feed_content.setHorizontallyScrolling(false);
        et_feed_content.setSingleLine(false);
    }

    @Override
    protected void initData() {
        maxInputFormat = getString(R.string.text_input_max_140);
        maxInput = String.format(maxInputFormat, "140");
        tv_font_count.setText(maxInput);
        tv_title.setText(getString(R.string.text_feedback));
    }

    @Override
    protected void initEvent() {
        et_feed_content.addTextChangedListener(mTextWatcher);
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.tv_post_feed)
    void submitFeed() {
        if (inputCheck()) {
            postFeed();
        }
    }

    @OnClick(R.id.ll_back) void back(){
        finish();
    }
    /**
     * 发送意见
     */
    public void postFeed() {

        OkHttpUtils.post().url(URL_FEEDBACK)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("device", tv_device.getText().toString())
                .addParams("version", tv_version.getText().toString())
                .addParams("content", et_feed_content.getText().toString())
                .addParams("contact", et_feed_phone.getText().toString())
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                ShowToast.showShortToast(getString(R.string.text_feedback_success));
                finish();
            }

            @Override
            public void onError(Call call, Exception e) {
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(FeedbackActivity.this, status_error);
            }
        });

    }

    private boolean inputCheck() {
        String content = et_feed_content.getText().toString().trim();
        String phone = et_feed_phone.getText().toString().trim();

        if (TextUtils.isEmpty(content)) {
            ShowToast.showShortToast(getString(R.string.text_feedback_content_empty));
            return false;
        }

        if (TextUtils.isEmpty(phone)) {
            ShowToast.showShortToast(getString(R.string.text_cantact_empty));
            return false;
        }

        return true;
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            int inputCount = 140 - s.length();
            maxInput = String.format(maxInputFormat, inputCount + "");
            tv_font_count.setText(maxInput);

            KLog.e(inputCount);
            if (s.length() >= 140) {
                ShowToast.showShortToast(getString(R.string.text_input_lenght));
                return;
            }
        }
    };
}
